<template>
    <div class="app-container">
        <!-- 文章状态 -->
        <el-row :gutter="24" style="color: #999;" class="mb15 p0">
            <el-col :span="1.5">
                状态
            </el-col>
            <el-col :span="1.5" :class="isActive('all')" @click.native="changeStatus('all')">
                全部
            </el-col>
            <el-col :span="1.5" :class="isActive('public')" @click.native="changeStatus('public')">
                公开
            </el-col>
            <el-col :span="1.5" :class="isActive('secret')" @click.native="changeStatus('secret')">
                私密
            </el-col>
            <el-col :span="1.5" :class="isActive('draft')" @click.native="changeStatus('draft')">
                草稿
            </el-col>
            <el-col :span="1.5" :class="isActive('delete')" @click.native="changeStatus('delete')">
                回收站
            </el-col>
        </el-row>
        <!-- 搜索栏 -->
        <el-form :model="queryParams" :inline="true" class="lh50" v-show="showSearch">
            <el-form-item label="名称">
                <el-input @keyup.enter="handleQuery" v-model="queryParams.keyword" style="width: 150px"
                    placeholder="请输入文章名称" clearable />
            </el-form-item>
            <el-form-item label="类型">
                <el-select v-model="queryParams.articleType" placeholder="请选择类型" clearable style="width: 130px;">
                    <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="标签">
                <el-select v-model="queryParams.tagId" placeholder="请选择标签" clearable filterable @visible-change="getTag"
                    style="width: 130px">
                    <el-option v-for="item in tagList" :key="item.id" :label="item.tagName" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="分类">
                <el-select v-model="queryParams.categoryId" placeholder="请选择分类" clearable filterable
                    @visible-change="getCategory" style="width: 130px">
                    <el-option v-for="item in categoryList" :key="item.id" :label="item.categoryName" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- 操作按钮 -->
        <el-row :gutter="10" class="pd0">
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="articleIdList.length === 0"
                    @click="handleDelete(undefined)">批量删除</el-button>
            </el-col>
            <el-col :span="1.5" v-if="queryParams.isDelete == 0">
                <el-button type="danger" plain icon="Delete" :disabled="articleIdList.length === 0"
                    @click="handleRecycle(undefined)">批量回收</el-button>
            </el-col>
            <el-col :span="1.5" v-if="queryParams.isDelete == 1">
                <el-button type="success" plain icon="Finished" :disabled="articleIdList.length === 0"
                    @click="handleRecycle(undefined)">批量恢复</el-button>
            </el-col>
            <RightToolBar :showSearch="showSearch"   @changeShow="changeShowSearch"  @queryTable="getList"></RightToolBar>
        </el-row>
        <!-- 表格展示 -->
        <el-table border :data="articleList" @selection-change="handleSelectionChange" v-loading="loading">
            <!-- 表格列 -->
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <!--缩略图-->
            <el-table-column prop="articleCover" label="缩略图" width="180" align="center">
                <template #default="scope">
                    <el-image class="article-cover" :src="scope.row.articleCover"></el-image>
                    <el-icon v-if="scope.row.status == 1" class="article-status-icon">
                        <View />
                    </el-icon>
                    <el-icon v-if="scope.row.status == 2" class="article-status-icon">
                        <Hide />
                    </el-icon>
                    <el-icon v-if="scope.row.status == 3" class="article-status-icon">
                        <EditPen />
                    </el-icon>
                </template>
            </el-table-column>
            <!-- 文章标题 -->
            <el-table-column prop="articleTitle" label="标题" align="center"></el-table-column>
            <!-- 文章分类 -->
            <el-table-column prop="categoryName" label="分类" width="90" align="center"></el-table-column>
            <!-- 文章标签 -->
            <el-table-column prop="tagVOList" label="标签" width="160" align="center">
                <template #default="scope">
                    <el-tag v-for="item of scope.row.tagVOList" :key="item.tagId"
                        style="margin-right: 0.2rem; margin-top: 0.2rem">
                        {{ item.tagName }}
                    </el-tag>
                </template>
            </el-table-column>
            <!-- 文章浏览量 -->
            <el-table-column prop="viewCount" label="浏览量" width="70" align="center">
            </el-table-column>
            <!-- 文章点赞量 -->
            <el-table-column prop="likeCount" label="点赞量" width="70" align="center">
            </el-table-column>
            <!-- 文章类型 -->
            <el-table-column prop="articleType" label="类型" width="70" align="center">
                <template #default="scope">
                    <el-tag v-if="scope.row.articleType == 1" type="success">原创</el-tag>
                    <el-tag v-if="scope.row.articleType == 2" type="danger">转载</el-tag>
                    <el-tag v-if="scope.row.articleType == 3" type="primary">翻译</el-tag>
                </template>
            </el-table-column>
            <!-- 文章置顶 -->
            <el-table-column prop="isTop" label="置顶" width="70" align="center">
                <template #default="scope">
                    <el-switch v-model="scope.row.isTop" style="--el-switch-on-color: #13ce66;"
                        :disabled="scope.row.isDelete == 1" :active-value="1" :inactive-value="0"
                        @change="handleTop(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <!-- 文章推荐 -->
            <el-table-column prop="isRecommend" label="推荐" width="70" align="center">
                <template #default="scope">
                    <el-switch v-model="scope.row.isRecommend" style="--el-switch-on-color: #13ce66;"
                        :disabled="scope.row.isDelete == 1" :active-value="1" :inactive-value="0"
                        @change="handleRecommend(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <!-- 创建时间 -->
            <el-table-column prop="createTime" width="130" label="创建时间" align="center">
                <template #default="scope">
                    <div class="create-time">
                        <el-icon>
                            <clock />
                        </el-icon>
                        <span style="margin-left: 10px">{{ formattedDate(scope.row.createTime) }}</span>
                    </div>
                </template>
            </el-table-column>
            <!-- 操作 -->
            <el-table-column width="220" label="操作" align="center">
                <template #default="scope">
                    <el-button type="text" class="el-icon-edit" link @click="handleEdit(scope.row.id)"
                        v-if="scope.row.isDelete == 0">
                        编辑
                    </el-button>
                    <el-button type="text" class="el-icon-delete-solid danger" link @click="handleDelete(scope.row.id)">
                        删除
                    </el-button>
                    <el-button type="text danger" class="el-icon-delete-solid danger" link @click="handleRecycle(scope.row.id)"
                        v-if="queryParams.isDelete == 0">
                        回收
                    </el-button>
                    <el-button type="text" class="el-icon-top-left success" link @click="handleRecycle(scope.row.id)"
                        v-if="queryParams.isDelete == 1">
                        恢复
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
      <Pagination
      :total="count"   
      :page.sync="queryParams.current" 
      :limit.sync="queryParams.size"     
      @pagination="getList" 
    />
    </div>
</template>

<script>
import {getArticleList, recommendArticle, topArticle, deleteArticle, recycleArticle} from  '@/api/article'
import {getTagList} from '@/api/tag'
import {getCategoryList} from '@/api/category'
import { formatDate } from "@/utils/date";
export default {
  data() {
    return {
    count: 0,
    showSearch: true,
    loading: false,
    title: "",
    queryParams: {
        current: 1,
        size: 10,
        isDelete: 0,
    },
    typeList: [
        {
            value: 1,
            label: "原创",
        },
        {
            value: 2,
            label: "转载",
        },
        {
            value: 3,
            label: "翻译",
        },
    ],
    activeStatus: "all",
    categoryList: [],
    tagList: [],
    articleIdList: [],
    articleList: [] 
}
  },
  computed:{
 
  },
  methods:{
    isActive(type){
       return this.activeStatus == type ? "active-status" : "status";
    },
    //获取文章列表
    getList(){
      this.loading = true;
      getArticleList(this.queryParams).then((res) => {
        if(res.code == 200){
          this.loading = false;
          this.articleList = res.data.recordList;
          this.count = res.data.count;
        }
      })
    },
    //获取标签列表
    getTag(){
      if(this.tagList.length == 0)
      {
        getTagList().then((res) => {
          if(res.code == 200){
            this.tagList = res.data;
          }
        })
      }
    },
    //获取分类列表
    getCategory(){
      if(this.categoryList.length == 0){
        getCategoryList().then((res) => {
          if(res.code == 200){
            this.categoryList = res.data;
          }
          })
      }
    },
    handleQuery(){
      this.queryParams.current = 1;
      this.getList();
    },
    //选项发生变化时候触发
    //将选中的行的id给articleIdList
    handleSelectionChange(selectedItems){
        this.articleIdList = selectedItems.map((item) => item.id);
    },
    //日期格式化
    formattedDate(date) {
      return formatDate(date);
    },
    //查询状态参数改变
    changeStatus(status){
      if(status == "all"){//查询所有
        this.queryParams.isDelete = 0;
        this.queryParams.status = undefined;
      }
      if(status == "public"){//查询公开
        this.queryParams.isDelete = 0;
        this.queryParams.status = 1;
      }
      if(status == "secret"){//查询私密
       this.queryParams.isDelete = 0;
        this.queryParams.status = 2;
      }
      if(status == "draft"){//查询草稿
        this.queryParams.isDelete = 0;
        this.queryParams.status = 3;
      }
      if(status == "delete"){//查询回收站
        this.queryParams.isDelete = 1;
        this.queryParams.status = undefined;
      }
      this.activeStatus = status;//对应状态高亮
      this.handleQuery();//查询
    },
    //置顶
    handleTop(data){
      const istop = data.isTop;
      topArticle({id:data.id, isTop: istop}).then((res) => {
        if(res.code == 200){
          if(istop){
          this.$message({type:'success',message:'置顶成功！'})
          }
          else{
              this.$message({type:'success',message:'取消置顶成功！'})
          }
          this.getList();
        }
      }).catch(()=>{ data.isTop = data.isTop === 0 ? 1 : 0;})
    },
    //推荐
    handleRecommend(data){
      const isRecommend = data.isRecommend;
      recommendArticle({id:data.id, isRecommend: isRecommend}).then((res) => {
        if(res.code == 200){
          if(isRecommend){
          this.$message({type:'success',message:'推荐成功！'})
          }
          else{
              this.$message({type:'success',message:'取消推荐成功！'})
          }
          this.getList();
        }
      }).catch(()=>{ data.isRecommend = data.isRecommend === 0 ? 1 : 0;})
    },
    //
    changeShowSearch(data){
      this.showSearch = data;
    },
    //删除文章、批量删除文章（不是逻辑删除）
    handleDelete(id){
      let deleteIds = [];
      if(id == undefined)
      {
        deleteIds = this.articleIdList;
        this.$confirm('此操作将永久删除选中的文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        }).then(() => {
          deleteArticle(deleteIds).then((res) => {
            if(res.code == 200){
              this.$message({type:'success',message:'删除成功！'})
              this.getList();
            }
          })
        }).catch(() => {
        this.$message({type:'info',message:'已取消删除选中文章！'})
        })
      }
      else
      {
      deleteIds.push(id);
        this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          deleteArticle(deleteIds).then((res) => {
            if(res.code == 200){
              this.$message({type:'success',message:'删除成功！'})
              this.getList();
            }
          })
      }).catch(() => {
        this.$message({type:'info',message:'已取消删除！'})
      })
      }
    },
    //回收文章、批量回收文章（逻辑删除）
    handleRecycle(id){
    let deleteDTO = {
        idList: [],
        isDelete: 0,
    };
    deleteDTO.isDelete = this.queryParams.isDelete === 0 ? 1 : 0;
    const text = deleteDTO.isDelete === 0 ? '恢复' : '回收';
    if(id == undefined)
    {
      deleteDTO.idList = this.articleIdList;
      this.$confirm('此操作将'+ text +'选中的文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        }).then(() => {
          recycleArticle(deleteDTO).then((res) => {
            if(res.code == 200){
              this.$message({type:'success',message:text + '成功！'})
              this.getList();
            }
          })
        }).catch(() => {
        this.$message({type:'info',message:'已取消'+text +'选中文章！'})
        })
    }
    else{
      deleteDTO.idList.push(id);
      this.$confirm('此操作将'+ text +'该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          recycleArticle(deleteDTO).then((res) => {
            if(res.code == 200){
              this.$message({type:'success',message:text + '成功！'})
              this.getList();
            }
          })
      }).catch(() => {
        this.$message({type:'info',message:'已取消'+text +'!'})
      })
    }
    },
    //编辑文章
    handleEdit(articleId){
      this.$router.push({path: `/article/write/${articleId}`});
    }
  },
  mounted(){
    this.getList();
  }
}
</script>

<style>
.status {
    cursor: pointer;
}

.mb15 {
    margin-bottom: 15px;
    cursor: pointer;
}

.pd0 {
    padding: 0px;

}
.p0 {
    padding: 0px;
}

.el-form-item {
    margin-bottom: 15px;
}

.active-status {
    cursor: pointer;
    color: #46a6ff;
    font-weight: bold;
    cursor: pointer;
}

.article-cover {
    position: relative;
    width: 100%;
    height: 90px;
    border-radius: 4px;
}

.article-cover::after {
    content: "";
    background: rgba(0, 0, 0, 0.3);
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

.article-status-icon {
    color: #fff;
    font-size: 1.5rem;
    position: absolute;
    right: 1rem;
    bottom: 1.4rem;
}
.danger{
  color: #f57878;
}
.danger:hover{
  color: #ee9797;
}

.success{
  color:#71c647;

}
.success:hover{
  color:#b3e19d;
}
</style>